package com.example.administrator.personalmail.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.personalmail.Activity.AddNewAddressActivity;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.greendao.AddressDao;
import com.example.administrator.personalmail.model.Address;
import com.example.administrator.personalmail.widget.GreenDaoManager;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter {
    private View view;
    private List<Address> mDatas;
    private Context mContext;

    public AddressAdapter(List<Address> list, Context context){
        mDatas = list;
        mContext = context;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.template_address,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.tvName.setText(mDatas.get(position).getName());
        viewHolder.tvPhone.setText(mDatas.get(position).getPhone());
        viewHolder.tvAdress.setText(mDatas.get(position).getAddress()+" " +mDatas.get(position).getAddressDetail());

    }

    @Override
    public int getItemCount() {
        if (mDatas == null) return 0;

        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView tvName;
        private TextView tvPhone;
        private TextView tvAdress;
        private TextView tvEdit;
        private TextView tvDelete;
        public ViewHolder(View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.txt_name_address);
            tvPhone = itemView.findViewById(R.id.txt_phone_address);
            tvAdress = itemView.findViewById(R.id.txt_address);
            tvEdit = itemView.findViewById(R.id.txt_edit_address);
            tvEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mEditAddressListenner.onClick(mDatas.get(getLayoutPosition()));
                }
            });
            tvDelete = itemView.findViewById(R.id.txt_del_address);
            tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int c = 0;
                    List<Address> list = new ArrayList<>();
                    for (Address address : mDatas){
                        if (c == getLayoutPosition()){
                            getAddressDao().delete(address);
                        }else {
                            list.add(address);
                        }
                        c++;
                    }
                    mDatas.clear();
                    mDatas.addAll(0,list);
                    notifyDataSetChanged();
                }
            });
        }
    }

    private EditAddressListenner mEditAddressListenner;
    public interface EditAddressListenner{
        public void onClick(Address address);
    }
    public void setEditAddressListenner(EditAddressListenner editAddressListenner){
        mEditAddressListenner = editAddressListenner;
    }

    public AddressDao getAddressDao()
    {
        return GreenDaoManager.getInstance().getSession().getAddressDao();
    }
}
